package com.skstudio.nuprint;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;

public class DnssdService {

	JmDNS mJmDns = null;

	ServiceInfo mIppService = null;

	ServiceInfo mLpdService = null;

	Context mContext = null;
	
	public DnssdService(Context ctx) {
		boolean log = true;
		if (log) {
			ConsoleHandler handler = new ConsoleHandler();
			handler.setLevel(Level.FINEST);
			for (Enumeration<String> enumerator = LogManager.getLogManager()
					.getLoggerNames(); enumerator.hasMoreElements();) {
				String loggerName = enumerator.nextElement();
				Logger logger = Logger.getLogger(loggerName);
				logger.addHandler(handler);
				logger.setLevel(Level.FINEST);
			}
		}
		mContext = ctx;
	}

	public void StopService() {
		 if (mMulticastLock != null) mMulticastLock.release();
		 
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					if (mJmDns == null) return;
					
					if (mIppService != null)
						mJmDns.unregisterService(mIppService);
					if (mLpdService != null)
						mJmDns.unregisterService(mLpdService);
					mJmDns.unregisterAllServices();

					mJmDns.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	MulticastLock mMulticastLock = null;
	
	public void StartService() {
		WifiManager wifi = (WifiManager) mContext.getSystemService(android.content.Context.WIFI_SERVICE);
		mMulticastLock = wifi.createMulticastLock("NuPrintMulticastLock");
		mMulticastLock.setReferenceCounted(true);
		mMulticastLock.acquire();
		        
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					mJmDns = JmDNS.create();

					ServiceInfo[] infos = mJmDns.list("_printer._tcp.local.");
					for (int i = 0; infos != null && i < infos.length; i++) {
						if (infos[i].getName().equals("Samsung ML-4550 Series")
								&& infos[i].getPort() == 5150) {
							mJmDns.close();
							return;
						}
					}

					final HashMap<String, String> values = new HashMap<String, String>();
					values.put("txtvers", "1");
					values.put("qtotal", "1");
					values.put("ty", "Samsung ML-4550 Series");
					values.put("product", "(Samsung ML-4550 Series)");
					values.put("pdl", "application/octet-stream,application/postscript");
					values.put("usb_MFG", "Samsung");
					values.put("usb_MDL", "ML-4550 Series");
					values.put("Transparent", "T");
					values.put("Binary", "T");
					values.put("Color", "T");
					values.put("Copies", "F");
					values.put("Duplex", "T");
					values.put("PaperCustom", "T");
					values.put("Bind", "F");
					values.put("Collate", "F");
					values.put("Sort", "F");
					values.put("Staple", "F");
					values.put("Punch", "0");

					mIppService = ServiceInfo.create("_ipp._tcp.local.",
						 "Samsung ML-4550 Series", "_printer", 6310, 0, 0,
						 values);
					mJmDns.registerService(mIppService);

					mLpdService = ServiceInfo.create("_printer._tcp.local.",
							"Samsung ML-4550 Series", "_printer", 5150, 0, 0,
							values);
					mJmDns.registerService(mLpdService);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
}
